﻿Imports System.Web

Namespace helper
    ''' <summary>
    ''' provides methods due with http
    ''' </summary>
    ''' <remarks></remarks>
    Public NotInheritable Class httpHelper

        ''' <summary>
        ''' perform http post
        ''' </summary>
        ''' <param name="URL">post url</param>
        ''' <param name="idata">query string that you want post to</param>
        ''' <returns>response from </returns>
        ''' <remarks></remarks>
        Public Shared Function Post(ByVal URL As String, ByVal idata As NameValueCollection) As String
            'input validation
            If String.IsNullOrEmpty(URL) Then
                Throw New ArgumentNullException("URL")
            End If

            If idata.Count = 0 Then
                Throw New ArgumentNullException("idata")
            End If


            Dim B As Byte() = Nothing
            Dim response As Byte() = Nothing
            Dim rev As String = String.Empty
            Dim data As String = String.Empty

            Dim beginFlag As Integer = 1

          

            For Each s As String In idata.AllKeys
                If beginFlag <> 1 Then
                    data &= "&" & s & "=" & HttpUtility.UrlEncode(idata(s))
                Else
                    beginFlag = 2
                    data &= s & "=" & HttpUtility.UrlEncode(idata(s))
                End If
            Next

            Try
                Using web As New System.Net.WebClient
                    web.Headers.Add("Content-Type", "application/x-www-form-urlencoded")
                    B = System.Text.Encoding.ASCII.GetBytes(data.ToString())
                    response = web.UploadData(URL, "POST", B)
                    rev = System.Text.Encoding.ASCII.GetString(response)
                End Using
            Catch ex As Exception
                Throw ex
            End Try

            Return rev

        End Function
        Public Shared Function [GET](ByVal URL As String, ByVal idata As NameValueCollection) As String
            'input validation
            If String.IsNullOrEmpty(URL) Then
                Throw New ArgumentNullException("URL")
            End If

            If idata.Count = 0 Then
                Throw New ArgumentNullException("idata")
            End If


            Dim B As Byte() = Nothing
            Dim response As Byte() = Nothing
            Dim rev As String = String.Empty
            Dim data As String = String.Empty

            Dim beginFlag As Integer = 1



            For Each s As String In idata.AllKeys
                If beginFlag <> 1 Then
                    data &= "&" & s & "=" & HttpUtility.UrlEncode(idata(s))
                Else
                    beginFlag = 2
                    data &= s & "=" & HttpUtility.UrlEncode(idata(s))
                End If
            Next

            Try
                Using web As New System.Net.WebClient
                    web.Headers.Add("Content-Type", "application/x-www-form-urlencoded")
                    B = System.Text.Encoding.ASCII.GetBytes(data.ToString())
                    response = web.UploadData(URL, "GET", B)
                    rev = System.Text.Encoding.ASCII.GetString(response)
                End Using
            Catch ex As Exception
                Throw ex
            End Try

            Return rev

        End Function

        ''' <summary>
        ''' retrieve data from http post or http get
        ''' </summary>
        ''' <param name="paraName">parameter name</param>
        ''' <param name="request">httprequest</param>
        ''' <returns>return value</returns>
        ''' <remarks></remarks>
        Public Shared Function Retrieve(ByVal paraName As String, ByVal request As HttpRequest) As String


            Dim strReturn As String = ""
            Try
                'get data with get method
                strReturn = request.QueryString(paraName)
                If strReturn = "" Then
                    'get data with post method
                    strReturn = System.Web.HttpUtility.UrlDecode(request.Form(paraName))
                End If
                If strReturn <> "" Then
                    strReturn = strReturn.Trim
                    Return strReturn
                Else
                    Return ""
                End If
            Catch ex As Exception
                Return ""
            End Try
        End Function
    End Class


End Namespace


